//
//  MyWalletViewController.m
//  MagicZhao
//
//  Created by hy002 on 2017/12/8.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "MyWalletViewController.h"
#import "WalletDetailModel.h"
#import "BalanceViewController.h"
#import "BankCarViewController.h"

static NSString *cellPatn = @"mywalletCell";
@interface MyWalletViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, strong)NSMutableArray *alldetailAry;
@property (nonatomic, strong)UILabel *balanceLab;
@property (nonatomic, assign)NSInteger page;

@end

@implementation MyWalletViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self requestMineWallet];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 0;
    _alldetailAry = [NSMutableArray arrayWithCapacity:0];
    [self creatUI];
    [self requestWalletTransaction];
}

- (void)creatUI
{
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = BacksColor;
    self.navigationItem.title = @"我的钱包";
//    UIImage *searchBtnImage = [[UIImage imageNamed:@"sz"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:searchBtnImage style:UIBarButtonItemStyleDone target:self action:@selector(walletAction)];
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 75*displaywidth)];
    topView.backgroundColor = RGB(157, 171, 205);
    [self.view addSubview:topView];
    UIView *lineAview = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-0.5*displaywidth, 0, 1*displaywidth, 75*displaywidth)];
    lineAview.backgroundColor = RGB(204, 204, 204);
    [topView addSubview:lineAview];
    NSArray *imgAry = @[@"yue",@"ka"];
    NSArray *titAry = @[@"余额 ￥0.00",@"银行卡"];
    for (int i = 0; i < imgAry.count; i++)
    {
        UIImageView *imgAView = [ZCControl createImageViewWithFrame:CGRectMake(SCREEN_WIDTH/4*(2*i+1)-15*displaywidth, 10*displaywidth, 30*displaywidth, 30*displaywidth) ImageName:imgAry[i]];
        imgAView.contentMode = UIViewContentModeScaleAspectFit;
        [topView addSubview:imgAView];
        UILabel *titALab = [ZCControl createLabelWithFrame:CGRectMake(SCREEN_WIDTH/2*i, 40*displaywidth, SCREEN_WIDTH/2, 35*displaywidth) Font:15.0 Text:titAry[i] Color:[UIColor whiteColor]];
        titALab.textAlignment = NSTextAlignmentCenter;
        [topView addSubview:titALab];
        if (i == 0)
        {
            _balanceLab = titALab;
        }
        UIButton *balanceBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH/2*i, 0, SCREEN_WIDTH/2, 75*displaywidth) ImageName:nil Target:self Action:@selector(closeTypeAction:) Title:nil Font:12.0];
        balanceBtn.tag = 50+i;
        balanceBtn.backgroundColor = [UIColor clearColor];
        [topView addSubview:balanceBtn];
    }
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 75*displaywidth, SCREEN_WIDTH, SCREEN_HEIGHT-SCREER_HeiX-STA_NAV_HEIGHT-75*displaywidth) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_tableview];
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewHeaderData)];
    self.tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreFootData)];
    self.tableview.mj_footer.automaticallyHidden = YES;
    self.tableview.estimatedRowHeight = 0;
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    

}

- (void)loadNewHeaderData
{
    _page = 0;
    [self requestWalletTransaction];
}

- (void)loadMoreFootData
{
    _page++;
    [self requestWalletTransaction];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _alldetailAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellPatn];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_alldetailAry.count > 0)
    {
        WalletDetailModel *model = _alldetailAry[indexPath.row];
        UILabel *namelab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 0, SCREEN_WIDTH-150*displaywidth, 30*displaywidth) Font:15.0 Text:model.title Color:RGB(77, 77, 77)];
        [cell.contentView addSubview:namelab];
        UILabel *timelab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 30*displaywidth, SCREEN_WIDTH-150*displaywidth, 15*displaywidth) Font:12.0 Text:model.createDtm Color:RGB(128, 128, 128)];
        [cell.contentView addSubview:timelab];
        NSString *moneyStr = [NSString stringWithFormat:@"+%@",model.money];
        UIColor *moneyColor = RGB(238, 103, 94);
        if ([model.type integerValue] == 2)
        {
            moneyStr = [NSString stringWithFormat:@"-%@",model.money];
            moneyColor = RGB(128, 128, 128);
        }
        UILabel *moneyLab = [ZCControl createLabelWithFrame:CGRectMake(SCREEN_WIDTH- 150*displaywidth, 5*displaywidth, 135*displaywidth, 40*displaywidth) Font:14.0 Text:moneyStr Color:moneyColor];
        moneyLab.textAlignment = NSTextAlignmentRight;
        [cell.contentView addSubview:moneyLab];
        UIView *lineview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1*displaywidth)];
        lineview.backgroundColor = LinesColor;
        [cell.contentView addSubview:lineview];
    }
    return cell;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30*displaywidth)];
    myView.backgroundColor = [UIColor whiteColor];
    UILabel *titLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 0, 120*displaywidth, 30*displaywidth) Font:13.0 Text:@"账单明细" Color:RGB(77, 77, 77)];
    [myView addSubview:titLab];
    return myView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50*displaywidth;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30*displaywidth;
}

- (void)closeTypeAction:(UIButton *)btn
{
    if (btn.tag == 50)
    {
        //余额
        BalanceViewController *vc = [[BalanceViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else
    {
        //银行卡
        BankCarViewController *vc = [[BankCarViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

//设置
//- (void)walletAction
//{
//
//}

- (void)requestMineWallet
{
    WEAKSELF;
    [HYBaseHandler requestMineWalletParmers:nil Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            NSString *money = obj[@"data"][@"money"];
            dispatch_async(dispatch_get_main_queue(), ^{
                double moneyD = [money doubleValue];
                weakSelf.balanceLab.text = [NSString stringWithFormat:@"余额 ￥%.2lf",moneyD];
            });
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}

- (void)requestWalletTransaction
{
    WEAKSELF;
    NSDictionary *dic = @{@"page":[NSString stringWithFormat:@"%ld",_page]};
    [HYBaseHandler requestWalletTransactionParmers:dic Success:^(id obj) {
        [weakSelf.tableview.mj_header endRefreshing];
        [weakSelf.tableview.mj_footer endRefreshing];
        if ([obj server_Getcodecheck])
        {
            NSArray *walletArray = [WalletDetailModel mj_objectArrayWithKeyValuesArray:obj[@"data"][@"content"]];
            if (weakSelf.page == 0)
            {
                [weakSelf.alldetailAry removeAllObjects];
            }
            [weakSelf.alldetailAry addObjectsFromArray:walletArray];
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.tableview reloadData];
            });
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        [weakSelf.tableview.mj_header endRefreshing];
        [weakSelf.tableview.mj_footer endRefreshing];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
